package com.beamfield.controller.api.mb;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.beamfield.common.ApiResult;
import com.beamfield.common.LogInfo;
import com.beamfield.controller.common.MbBaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.enums.QrcodeEnum;
import com.beamfield.modelVo.IssueVo;
import com.beamfield.modelVo.Qrcode;
import com.beamfield.modelVo.QrcodeVo;
import com.beamfield.service.manage.material.IssueService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author Zephyr
 * @Description: 移动端 - - 发料单
 * @Date 2018/10/23
 */
@Controller
@RequestMapping(value = "/mb_api/issue")
public class MbIssueController extends MbBaseController {

    private final IssueService issueService;

    @Autowired
    public MbIssueController(IssueService issueService) {
        this.issueService = issueService;
    }

    /**
     * 根据发料单获取发料单详情
     * @param request
     * @return
     */
    @RequestMapping(value = "/getOneIssue", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getOneIssue(HttpServletRequest request){
        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        String issueId = body.getString("issueId");
        if(StringUtils.isEmpty(issueId)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        try{
            return  issueService.getOneIssue(issueId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 发料
     * @param request
     * @return
     */
    @RequestMapping(value = "/doIssue", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doIssue(HttpServletRequest request){
        Long beamId = super.getLogInfo(request).getBeamId();
        Long userId = super.getLogInfo(request).getUserId();

        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        String issueId = body.getString("issueId");
//        String inbound = body.getString("inbound");
        try{
            return issueService.doIssue(issueId,null,beamId,userId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 预埋件出库
     * @param request
     * @return
     */
    @RequestMapping(value = "/embedOutbound", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult embedOutbound(HttpServletRequest request){
        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        String issueId = body.getString("issueId");
        JSONArray embeds = body.getJSONArray("embeds");
        if(StringUtils.isEmpty(issueId) || CollectionUtils.isEmpty(embeds)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);
        try{
            return issueService.embedOutbound(issueId, embeds, userId, beamId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }
    /**
     * 根据二维码信息获取设备信息
     * @return
     */
    @RequestMapping(value = "/getEmbedData", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getEmbedData(HttpServletRequest request) {
        JSONObject body = super.getBody(request);
        if (body == null) {
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        String qrCodeJson = body.getString("qrCodeJson");
        String issueId = body.getString("issueId");
        Qrcode qrcode = super.getQrcode(qrCodeJson);
        if (qrcode == null) {
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), "无法解析二维码信息");
        }
        LogInfo logInfo = super.getLogInfo(request);
        Long beamId = logInfo.getBeamId();
        if (!Objects.equals(qrcode.getBeam(), String.valueOf(beamId))) {
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), "此二维码不属于当前登录梁场");
        }
        try {
            String type = qrcode.getType();
            //预埋件入库id
            String data = qrcode.getData();
            if (!Objects.equals(type, QrcodeEnum.MATERIAL_EMBED.getType())) {
                return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), "请扫描此发料单的预埋件二维码");
            }
            return issueService.getMbEmbedData(Long.parseLong(issueId), Long.parseLong(data));
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }
}
